import http from '@/api/api'
import { defineStore } from 'pinia'

export const mainStore = defineStore('main', {
    // 开启数据持久化
    // persist: true,
    // 持久化存储插件其他配置
    persist: {
        // 修改存储中使用的键名称，默认为当前 Store的 id
        key: 'storekey',
        // 修改为 sessionStorage，默认为 localStorage
        storage: window.sessionStorage,
        // 部分持久化状态的点符号路径数组，[]意味着没有状态被持久化(默认为undefined，持久化整个状态)
        paths: ['nested.data'],
    },
    state() {
        return {
            audio: new Audio(),
            isPlay: false as boolean,  //类型推断

        }
    },
    getters: {
    },
    actions: {
        async playAudio(id:number) {
            let {data} = await http.get<{data:string}>('/song/url?id='+id);
            console.log(data)

            this.audio.src = data
            this.audio.play().then(()=>{
                console.log('播放成功')
                this.isPlay = true
            })
        },
        togglePlayAudio() {
            this.audio.pause()
            this.isPlay = !this.isPlay;
            if (this.isPlay) {
                this.audio.pause()
            }else {
                this.audio.play()
            }
        }
    }
})
